﻿using System.ComponentModel;
using System.Drawing;
using System.Text.Json.Serialization;
using Newtonsoft.Json;
using ProjectN.Core.Element.Def;
using ProjectN.Core.Fore.Editor;
using ProjectN.Core.Instance.Data;

namespace ProjectN.Core.Element
{
    static partial class Utilities
    {
        public static System.Drawing.Size GetSize(this Image image)
        {
            return new System.Drawing.Size(image.Width, image.Height);
        }
    }
    [JsonObject(MemberSerialization = MemberSerialization.OptIn)]
    public class SimpleImage : BasicElement
    {
        [Category("基本"), DisplayName("位置"), JsonProperty]
        public required Point Position { get; set; }

        [Category("基本"), DisplayName("图片"), JsonProperty]
        public required ImageInfo Image { get; set; }

        public SimpleImage()
        {
            this.Draw += SimpleImage_Draw;
            this.Load += SimpleImage_Load;
        }

        private void SimpleImage_Draw(object sender, DrawEventArgs e)
        {
            if (e.Color.HasValue)
                e.Graphics.FillRectangle(
                    new SolidBrush(e.Color.Value),
                    new System.Drawing.Rectangle(Position, Image.Create().GetSize())
                 );
            else
                e.Graphics.DrawImageUnscaled(Image.Create(), Position);
        }

        private void SimpleImage_Load(object sender, LoadEventArgs e)
        {
            this.Data = e.InstanceData;
        }

        protected override void UpdateData(InstanceData data)
        {
            Image.Data = data;
        }

        public override string ToString() => "图片";
    }

    [JsonObject(MemberSerialization= MemberSerialization.OptIn)]
    public class RectedImage : BasicElement
    {
        [Category("基本"), DisplayName("目标矩形"), JsonProperty]
        public required RRectangle Rect { get; set; }

        [Category("基本"), DisplayName("图片"), JsonProperty]
        public required ImageInfo Image { get; set; }

        public RectedImage()
        {
            this.Draw += RectedImage_Draw;
            this.Load += RectedImage_Load;
        }

        private void RectedImage_Draw(object sender, DrawEventArgs e)
        {
            if (e.Color.HasValue)
                e.Graphics.FillRectangle(
                    new SolidBrush(e.Color.Value),
                    this.Rect
                );
            else
                e.Graphics.DrawImageUnscaled(Image.Create(), Rect);
        }

        private void RectedImage_Load(object sender, LoadEventArgs e)
        {
            this.Data = e.InstanceData;
        }

        protected override void UpdateData(InstanceData data)
        {
            Image.Data = data;
        }

        public override string ToString() => "图片";
    }

    [JsonObject(MemberSerialization = MemberSerialization.OptIn)]
    public class ScaledImage : BasicElement
    {
        [Category("基本"), DisplayName("目标矩形"), JsonProperty]
        public required RRectangle Dest { get; set; }

        [Category("基本"), DisplayName("原图矩形"), JsonProperty]
        public required RRectangle Src { get; set; }

        [Browsable(false), JsonProperty]
        public required GraphicsUnit Unit { get; set; }

        [Category("基本"), DisplayName("图片"), JsonProperty]
        public required ImageInfo Image { get; set; }
        public ScaledImage()
        {
            this.Draw += ScaledImage_Draw;
            this.Load += ScaledImage_Load;
        }

        private void ScaledImage_Draw(object sender, DrawEventArgs e)
        {
            if (e.Color.HasValue)
                e.Graphics.FillRectangle(
                    new SolidBrush(e.Color.Value),
                    this.Dest
                );
            else
                e.Graphics.DrawImage(Image.Create(), Dest, Src, Unit);
        }

        private void ScaledImage_Load(object sender, LoadEventArgs e)
        {
            this.Data = e.InstanceData;
        }

        protected override void UpdateData(InstanceData data)
        {
            Image.Data = data;
        }

        public override string ToString() => "图片";
    }
}
